package org.tissou.client.Panel_Premier.gestion_tab.Operations_de_gestion;

import org.tissou.client.Interface.Base_Panel;
import org.tissou.client.Services.ArbreDeCategories;
import org.tissou.client.Services.NouvelleCategorieService;
import org.tissou.client.Services.CategorieInfos;
import org.tissou.client.Services.MonException;
import org.tissou.client.widgets.Label_MessageInformation;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.Timer;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 03/03/2009
 * Time: 21:12:32
 * To change this template use File | Settings | File Templates.
 */
public class Nouvelle_Categorie extends Base_Panel {
  DockPanel principal = new DockPanel();
  Label_MessageInformation message_information = new Label_MessageInformation();

   Timer t = new Timer(){
      public void run() {
        message_information.setMessage(Label_MessageInformation.Etat.NEUTRE,"");
      }
  };

  DockPanel panel_categories = new DockPanel();
  Label label_categories = new Label("Choisis la categorie parent de la nouvelle categorie");
  ArbreDeCategories arbre_categories = new ArbreDeCategories();

  DockPanel panel_nouvellecategorie = new DockPanel();
  Label label_nouvellecategorie = new Label("Quel est le nom de la nouvelle categorie ?");
  TextBox txtbox_nouvellecategorie = new TextBox();

  DockPanel panel_go = new DockPanel();
  Label label_go = new Label("C'est parti, je tente ma chance en cliquant sur le bouton la");
  Button button_go = new Button("GO");

  protected void MiseEnPlaceStyles() {

  }

  protected void MiseEnPlaceParametresWidgets() {

  }

  protected void MiseEnPlaceParametresPanel() {

    panel_categories.add(label_categories, DockPanel.NORTH);
    panel_categories.add(arbre_categories, DockPanel.NORTH);
    panel_categories.addStyleName("dockPanelCategories");

    panel_nouvellecategorie.add(label_nouvellecategorie, DockPanel.NORTH);
    panel_nouvellecategorie.add(txtbox_nouvellecategorie, DockPanel.NORTH);
    panel_categories.addStyleName("dockPanelLabelWidget");

    panel_go.add(label_go, DockPanel.NORTH);
    panel_go.add(button_go, DockPanel.NORTH);
    panel_go.addStyleName("dockPanelLabelWidget");
  }

  protected void MiseEnPlaceActions() {
    button_go.addClickListener(new ClickListener(){

      public void onClick(Widget sender) {
        message_information.setMessage(Label_MessageInformation.Etat.SERVICEATTENTE, "En attente d'une reponse du serveur.");
        final AsyncCallback callback = new AsyncCallback(){

          public void onFailure(Throwable caught) {
            System.out.println("Creer categorie: Ca a pas marche pas de chance. Erreur:");
            System.out.println(caught.getLocalizedMessage());
            message_information.setMessage(Label_MessageInformation.Etat.SERVICEPASMARCHE, caught.getLocalizedMessage());
          }

          public void onSuccess(Object result) {
            System.out.println("Creer categorie: Ca a marche !");
            arbre_categories.Refresh();
            message_information.setMessage(Label_MessageInformation.Etat.SERVICEMARCHE, "La nouvelle categorie a ete creee !!");
            t.schedule(3000);
          }
        };

        CategorieInfos categorie_parent;
        try{categorie_parent = arbre_categories.donneLaCategorieSelectionnee();
        } catch (MonException e){
          message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE, e.getMessage());
          throw new MonException(e.getMessage());
        }
        String nouvellecategorie_nom = txtbox_nouvellecategorie.getText();

        if ("".equals(nouvellecategorie_nom)){
          message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,"Tu n'as pas donne le nom de la nouvelle categorie.");
        }
        else{
          NouvelleCategorieService.App.getInstance().CreerNouvelleCategorie(nouvellecategorie_nom,
            categorie_parent, callback);
        }

      }
    });
  }

  protected void MiseEnPlacePanels() {
    principal.setVerticalAlignment(DockPanel.ALIGN_TOP);
    principal.add(message_information, DockPanel.NORTH);
    principal.setCellHeight(message_information, "40px");
    principal.add(panel_categories, DockPanel.WEST);
    principal.add(panel_nouvellecategorie, DockPanel.NORTH);
    principal.add(panel_go, DockPanel.NORTH);
    
  }

  protected Panel donneLePanelPrincipal() {
    return principal;
  }
}
